package com.ruoyi.ucar.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author Administrator
 */
public class UrlToImgSaveUtil {

    /**
     * 构建js文件
     * @param url
     * @param picname
     * @param jsname
     */
    public static void  reload(String url,String picname,String jsname){
        //这里面的路径都是相对路径
        String content = "";
        //netToPicMoban.js这个phantomjs 的一个js模版，修改相应的参数就可以实现我们要的功能
        String str = read(new File("D:/template/NetToPicMoban.js"));
        String content1 = str.replace("url", "'"+url+"'");
        content = content1.replace("savename", "'"+picname+"'");
        write(content,"D:/template/", jsname );
    }

    /**
     * 文件读取
     * @param file
     * @return
     */
    public static String read(File file) {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] b = new byte[(int) file.length()];
            fis.read(b);
            return new String(b, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 文件写入
     * @param content
     * @param dirPath
     * @param fileName
     */
    public static void write(String content, String dirPath, String fileName) {
        File file = createFile(dirPath, fileName);
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件
     * @param dirPath
     * @param fileName
     * @return
     */
    private static File createFile(String dirPath, String fileName) {
        String filePath = "";
        if (Objects.isNull(dirPath) || dirPath.isEmpty()) {
            filePath = fileName;
        } else {
            if (dirPath.endsWith("/")) {
                filePath = dirPath + fileName;
            } else {
                filePath = dirPath + "/" + fileName;
            }
            File dir = new File(dirPath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }


    /**
     * 执行cmd
     * @param cmd
     */
    public static void  implcmd(String cmd){//在java中调用执行cmd命令
        Process p;
        System.out.println(cmd);
        try {
            p = Runtime.getRuntime().exec(cmd);
            // 等待进程执行完成
            int exitCode = p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            // 打印命令执行结果
            System.out.println("Command executed with exit code: " + exitCode);
        } catch (IOException e) {
            System.out.println("e==="+e.getMessage());
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args){
        String url = "https://www.baidu.com" ;
        String picname = System.currentTimeMillis()+"sina.png" ;
        String jsname = "sina.js";
        //生成js
        UrlToImgSaveUtil.reload( url, picname,jsname);
        //调用系统的cmd 执行phantomjs.exe
        String cmd1 = "cmd /c F: && cd F:\\tools\\phantomjs-2.1.1-windows\\bin\\" ;
        String cmd  = cmd1 + " && phantomjs.exe " + "D:\\template\\"+jsname;
        //执行cmd
        UrlToImgSaveUtil.implcmd(cmd);
    }
}

